<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>

	<!--bootstrap-toastr-->
	<script type="text/javascript" src="/bootstrap/toastr/toastr.min.js"></script>
	<link rel="stylesheet" type="text/css" href="/bootstrap/toastr/toastr.css">

	<!--bootStrap-fileinput-->
	<script type="text/javascript" src="/bootstrap/bootstrap-fileinput/js/fileinput.min.js"></script>
	<script type="text/javascript" src="/bootstrap/bootstrap-fileinput/js/locales/zh.js"></script>
	<link rel="stylesheet" type="text/css" href="/bootstrap/bootstrap-fileinput/css/fileinput.min.css">

	<script>

		$(function () {
			$('#uploadImg').fileinput({
				language: 'zh',
				uploadUrl: "/oss/upload",
				browseClass: 'btn btn-primary',
				maxFileCount: 1,//最多支持上传的个数
				enctype: 'multipart/form-data',
				maxFileSize: 1024, //单位为kb, 如果为0表示不限制文件大小
				allowedFileExtensions: ['jpg', 'gif', 'png', 'jpeg']
			});

			$("#uploadImg").on("fileuploaded", function(event, data, previewId, index) {
				if(data.response.code == 200){
					$("#insertPic").val(data.response.data);
				}
			});
		});

		$(function () {
			getListContent();
		});
		function getListContent() {
			$.ajax({
				url : "/content/getListContent",
				dataType : "json",
				type : "post",
				data : {pageNumber : $("#pageNumber").val()},
				success : function (returnData) {
					var result = returnData.data.list;
					console.log(result);
					var table = "";
					for (var i = 0; i < result.length; i++) {
						table += "<tr>"
						table += "<td><input name='box' value='"+ result[i].id +"' type='checkbox'></td>"
						table += "<td>" + result[i].id + "</td>"
						table += "<td>" + result[i].categoryId + "</td>"
						table += "<td>" + result[i].title + "</td>"
						table += "<td>" + result[i].url + "</td>"
						table += "<td><img alt='' src='" + result[i].pic +" ' width='30%'></td>"
						table += "<td>" + result[i].sortOrder + "</td>"
						table += "<td>" + result[i].status + "</td>"
						table += "<td class='text-center'>"
						table += "<button onclick='toUpdateContent("+ result[i].id +")' type='button' class='btn bg-olive btn-xs' data-toggle='modal' data-target='#updateContentModal'>修改</button>"
						table += "</td>"
						table += "</tr>"
					}

					$("#contentTable").html(table);

					$("#showCount").html(returnData.data.total)
					$("#showPage").html(returnData.data.pages)

					$("#totalPage").val(returnData.data.pages)
					$("#totalCount").val(returnData.data.total)
					$("#pageNumber").val(returnData.data.pageNumer)

					var rowPages = ""
					for (var j = 1;j <= returnData.data.pages;j++){
						rowPages += "<li>"
						rowPages += "<a href='javascript:changePageNumber("+ j +")'>"+ j +"</a>"
						rowPages += "</li>"
						$("#pages").html(rowPages)
					}
				},
				error : function (returnData) {
					toastr.error(returnData.message)
				}
			})
		}

		function getPage(flag) {
			var pageNumber = parseInt($("#pageNumber").val());
			var totalPage = parseInt($("#totalPage").val());

			if (flag == 1){
				if (pageNumber <= 1){
					toastr.warning("已经是第一页");
					return;
				}
				$("#pageNumber").val(1);
			}else if (flag == 2) {
				if (pageNumber <= 1){
					toastr.warning("已经是第一页");
					return;
				}
				$("#pageNumber").val(pageNumber - 1);
			}else if (flag == 3){
				if (pageNumber >= totalPage){
					toastr.warning("已经是最后一页");
					return;
				}
				$("#pageNumber").val(pageNumber + 1);
			} else if (flag == 4) {
				if (pageNumber >= totalPage){
					toastr.warning("已经是最后一页");
					return;
				}
				$("#pageNumber").val(totalPage);
			}
			getListContent();
		}

		function changePageNumber(obj) {
			$("#pageNumber").val(obj);
			getListContent();
		}

		function reload() {
			window.location.reload();
		}
		
		function insertContent() {
			$.ajax({
				url : "/content/insertContent",
				type : "post",
				data : $("#addForm").serialize(),
				dataType : "json",
				success : function (returnData) {
					if (returnData.code == 200) {
						toastr.success(returnData.message);
					} else {
						toastr.error(returnData.message);
					}
					setTimeout(function(){
								reload();
							},500
					);
				},
				error : function (returnData) {
					toastr.error("系统错误");
				}
			});
		}

		function toUpdateContent(id) {
			$.ajax({
				url : "/content/toUpdateContent",
				type : "post",
				data : {id : id},
				dataType : "json",
				success : function (returnData) {
					var data = returnData.data;
					$("#id").prop("value", id);
					$("#categoryId").prop("value", data.categoryId);
					$("#title").prop("value", data.title);
					$("#url").prop("value", data.url);
					$("#updatePic").val(data.pic);
					showContentImg(data.pic);
					$("#sortOrder").prop("value", data.sortOrder);
					if (data.status == 1){
						$("#status1").prop("checked", "checked");
					}else {
						$("#status0").prop("checked", "checked");
					}

				},
				error : function (returnData) {
					toastr.error("系统错误");
				}
			});
		}

		function showContentImg(src) {
			$('#uploadImg2').fileinput({
				language: 'zh',
				uploadUrl: "/oss/upload",
				browseClass: 'btn btn-primary',
				maxFileCount: 1,//最多支持上传的个数
				enctype: 'multipart/form-data',
				maxFileSize: 1024, //单位为kb, 如果为0表示不限制文件大小
				allowedFileExtensions: ['jpg', 'gif', 'png', 'jpeg'],
				initialPreviewAsData: true, // 是否初始化预览图片
				initialPreviewFileType: 'image', //回显文件的类型(初始化预览图片类型)
				initialPreview: src//图片地址
			});

			$("#uploadImg2").on("fileuploaded", function(event, data, previewId, index) {
				if(data.response.code == 200){
					$("#updatePic").val(data.response.data);
				}
			});
		}
		
		function updateContentInfo() {
			$.ajax({
				url : "/content/updateContentInfo",
				type : "post",
				data : $("#updateForm").serialize(),
				dataType : "json",
				success : function (returnData) {
					if (returnData.code == 200) {
						toastr.success(returnData.message);
					} else {
						toastr.error(returnData.message);
					}
					setTimeout(function(){
								reload();
							},500
					);
				},
				error : function (returnData) {
					toastr.error("系统错误");
				}
			});
		}

		// 删除
		function deleteContent() {
			if (confirm("确定要删除选中数据吗?")) {
				var names = $("[name=box]:checked");
				var ids = [];
				$(names).each(function (i, e) {
					ids.push(e.value)
				});
				if (ids <= 0) {
					toastr.warning("请选择要删除的广告")
					return;
				}
				$.ajax({
					url : "/content/deleteBatch",
					type : "post",
					data : {ids : ids},
					dataType : "json",
					success : function (returnData) {
						if (returnData.code == 200) {
							toastr.success(returnData.message);
						} else {
							toastr.error(returnData.message);
						}
						setTimeout(function(){
									reload();
								},500
						);

					},
					error : function (returnData) {
						alert(returnData.message);
					}
				});
			} else {
				return;
			}
		}

		function updateStatus(o) {
			var names = $("[name=box]:checked");
			var ids = [];
			$(names).each(function (i, e) {
				ids.push(e.value)
			});
			if (ids <= 0) {
				toastr.warning("请选择广告")
				return;
			}
			$.ajax({
				url : "/content/updateStatusById",
				type : "post",
				data : {ids : ids,status : o},
				dataType : "json",
				success : function (returnData) {
					if (returnData.code == 200) {
						toastr.success(returnData.message);
					} else {
						toastr.error(returnData.message);
					}
					setTimeout(function(){
								reload();
							},500
					);
				},
				error : function (returnData) {
					toastr.error("系统错误");
				}
			});
		}
	</script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#insertContentModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteContent()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='updateStatus(1)'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='updateStatus(2)'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="contentTable">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<div align="center">
								<nav aria-label="Page navigation">
									<input type="hidden" id="pageNumber" name="pageNumber">
									<input type="hidden" id="totalPage">
									<div class="page_num_inf">
										<i></i> 共
										<span id="showPage"></span>页<span id="showCount"></span>条
									</div>
									<ul class="pagination">
										<li>
											<a href="javascript:getPage(1);" aria-label="Previous">
												<span aria-hidden="true">首页</span>
											</a>

											<a href="javascript:getPage(2);" aria-label="Previous">
												<span aria-hidden="true">&laquo;上一页</span>
											</a>
										</li>
									</ul>
									<ul class="pagination" id="pages">

									</ul>
									<ul class="pagination">
										<li>
											<a href="javascript:getPage(3);" aria-label="Next">
												<span aria-hidden="true">下一页&raquo;</span>
											</a>
											<a href="javascript:getPage(4);" aria-label="Previous">
												<span aria-hidden="true">尾页</span>
											</a>
										</li>
									</ul>
								</nav>
							</div>
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<form id="addForm">
	<div class="modal fade" id="insertContentModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h3 id="myInsertModalLabel">广告添加</h3>
			</div>
			<div class="modal-body">

				<table class="table table-bordered table-striped"  width="800px">
					<tr>
						<td>广告分类</td>
						<td>
							<select class="form-control" name="categoryId" >
								<option th:each="listCate : ${categoryList}" th:value="${listCate.id}">[[${listCate.name}]]</option>
							</select>
						</td>
					</tr>
					<tr>
						<td>标题</td>
						<td><input name="title" class="form-control" placeholder="标题" >  </td>
					</tr>
					<tr>
						<td>URL</td>
						<td><input name="url" class="form-control" placeholder="URL" >  </td>
					</tr>
					<tr>
						<td>排序</td>
						<td><input name="sortOrder" class="form-control" placeholder="排序" >  </td>
					</tr>
					<tr>
						<td>广告图片</td>
						<td>
							<table>
								<tr>
									<td>
										<label for="uploadImg">广告图片预览</label>
										<input type="file" id="uploadImg" name="myFile">
										<input type="hidden" class="form-control" id="insertPic" name="pic" readonly="readonly">
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>是否有效</td>
						<td>
							<input name="status" type="radio" class="icheckbox_square-blue" value="0">无效
							<input name="status" type="radio" class="icheckbox_square-blue" value="1">有效
						</td>
					</tr>
				 </table>

			</div>
			<div class="modal-footer">
				<button class="btn btn-success" data-dismiss="modal" onclick="insertContent()" aria-hidden="true">保存</button>
				<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			</div>
		  </div>
		</div>
	</div>
</form>
  <form id="updateForm">
	  <!-- 编辑窗口 -->
	  <div class="modal fade" id="updateContentModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		  <div class="modal-dialog" >
			  <div class="modal-content">
				  <div class="modal-header">
					  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					  <h3 id="myUpdateModalLabel">广告编辑</h3>
				  </div>
				  <div class="modal-body">

					  <table class="table table-bordered table-striped"  width="800px">
						  <input type="hidden" name="id" id="id">
						  <tr>
							  <td>广告分类</td>
							  <td>
								  <select class="form-control" id="categoryId" name="categoryId" >
									  <option th:each="listCate : ${categoryList}" th:value="${listCate.id}">[[${listCate.name}]]</option>
								  </select>
							  </td>
						  </tr>
						  <tr>
							  <td>标题</td>
							  <td><input type="text" id="title" name="title" class="form-control" placeholder="标题" >  </td>
						  </tr>
						  <tr>
							  <td>URL</td>
							  <td><input type="text" id="url" name="url" class="form-control" placeholder="URL" >  </td>
						  </tr>
						  <tr>
							  <td>排序</td>
							  <td><input type="text" id="sortOrder" name="sortOrder" class="form-control" placeholder="排序" >  </td>
						  </tr>
						  <tr>
							  <td>广告图片</td>
							  <td>
								  <table>
									  <tr>
										  <td>
											  <label for="uploadImg">广告图片预览</label>
											  <input type="file" id="uploadImg2" name="myFile">
											  <input type="hidden" class="form-control" id="updatePic" name="pic" readonly="readonly">
										  </td>
									  </tr>
								  </table>
							  </td>
						  </tr>
						  <tr>
							  <td>是否有效</td>
							  <td>
								  <input name="status" id="status0" type="radio" class="icheckbox_square-blue" value="0">无效
								  <input name="status" id="status1" type="radio" class="icheckbox_square-blue" value="1">有效
							  </td>
						  </tr>
					  </table>

				  </div>
				  <div class="modal-footer">
					  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateContentInfo()">保存</button>
					  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
				  </div>
			  </div>
		  </div>
	  </div>
  </form>
</body>

</html>